﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using PremisPublisher.Common;
using PremisPublisher.Messages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace PremisPublisher.ViewModel
{
    /// <summary>
    /// 添加目录，批量加密目录内文件
    /// 作者：张鹏
    /// 日期：2016.5
    /// </summary>
    public class CryptDirectoryFilesViewModel : ViewModelBase
    {
        private ObservableCollection<string> _list_directorys;

        public ObservableCollection<string> ListDirectorys
        {
            get { return _list_directorys; }
            set { _list_directorys = value; RaisePropertyChanged("ListDirectorys"); }
        }

        private bool _busying;

        /// <summary>
        /// 进度条组件状态
        /// </summary>
        public bool Busying
        {
            get { return _busying; }
            set { _busying = value; RaisePropertyChanged("Busying"); }
        }

        private String busyingText;

        /// <summary>
        /// 进度条组件文字提示
        /// </summary>
        public String BusyingText
        {
            get { return busyingText; }
            set { busyingText = value; RaisePropertyChanged("BusyingText"); }
        }

        public ICommand AddDriectoryCommand { get; set; }
        public ICommand CryptDirectoryCommand { get; set; }
        public ICommand DelDriectoryCommand { get; set; }

        public CryptDirectoryFilesViewModel()
        {
            ListDirectorys = new ObservableCollection<string>();
            AddDriectoryCommand = new RelayCommand(AddDriectory);
            CryptDirectoryCommand = new RelayCommand(CryptDirectory);
            DelDriectoryCommand = new RelayCommand(DelDriectory);
        }

        private void DelDriectory()
        {
            Messenger.Default.Send(new DelFolderChoosenMessage() { Message = "确定删除所选目录？" });
        }

        private void AddDriectory()
        {
            Messenger.Default.Send(new ShowFolderChoosenMessage() { Message = "选择要加密的目录" });
        }

        /// <summary>
        /// 遍历目录实现文件rSA加密
        /// </summary>
        private void CryptDirectory()
        {
            BackgroundWorker crypt_worker = new BackgroundWorker();
            crypt_worker.RunWorkerCompleted += crypt_worker_RunWorkerCompleted;
            crypt_worker.DoWork += crypt_worker_DoWork;
            crypt_worker.WorkerReportsProgress = true;
            crypt_worker.ProgressChanged += crypt_worker_ProgressChanged;

            crypt_worker.RunWorkerAsync(ListDirectorys);
        }

        private void crypt_worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var dirs = e.Argument as ObservableCollection<string>;
            var c = crypt_sql_files_reader.GetInstance();

            List<string> all_files = new List<string>();

            BusyingText = "正在收集文件信息...";
            Busying = true;

            foreach (var d in dirs)
            {
                var sql_files = System.IO.Directory.GetFiles(d, "*.sql");
                all_files.AddRange(sql_files);
            }

            int i = 0;
            foreach (var file in all_files)
            {
                c.AddFile(file);

                int percent = (int)(((double)i++ / (double)((long)all_files.Count)) * 100);
                (sender as BackgroundWorker).ReportProgress(percent, file);
            }
        }

        private void crypt_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            BusyingText = string.Format("{0} 加密完成，当前进度: {1}%", e.UserState.ToString(), e.ProgressPercentage);
        }

        private void crypt_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Busying = false;
        }
    }
}
